/* -*- mode: c++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil; -*- */

#ifndef _PROCESS_H_
#define _PROCESS_H_

#include <string>

#include <utility.h>

#define PROC_WAIT               0
#define PROC_NO_WAIT            1

#define PROC_EXIT_NORMAL        0
#define PROC_EXIT_SIGNAL        1
#define PROC_EXIT_CORE_DUMP     2
#define PROC_EXIT_NOT_START     3

#define PROC_BIND_NONE          0
#define PROC_BIND_PIPE          1
#define PROC_BIND_FD            2

class Process {
public:
    // data struct sys-dep
    void               *_sys;

    void               *_user_data;

    Uint64              _run_ticks;

    std::string         _name;

    Process();

    ~Process();

    bool run(std::string name,
             Strings&    args,
             int*        sin       = NULL,
             int*        sout      = NULL,
             int*        serr      = NULL,
             int         exit_code = 1);

    bool running();

    // return bytes writed
    int write_stdin(const char *buf);

    // return bytes read
    int read_stdout(char *buf, int size);

    int read_stderr(char *buf, int size);

    // return -1 when sys-call error
    int wait_proc(int  flag,
                  int *exited,
                  int *exit_type,
                  int *exit_value);

    void clean_resources();

    void close_stdin();

    void send_signal(int signo);
};

#endif /* _PROCESS_H_ */
